<?php
require_once('MySqlConnection.php');
require_once('User.php');
require_once('Doctor.php');
require_once('District.php');
require_once('Location.php');
class PinkPonyDAL{ 
	private $_connection;
	//Singleton pattern
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new PinkPonyDAL();
		return self::$_instance;
	}
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
		
	public function GetUsers(){
		$this->_connection->Open();
		$sqlString = 'select * from gebruikers';
		$resultData = $this->_connection->ExecuteSQL($sqlString,'O');
		return $this->Mapping($resultData,'user');

	}
	
	public function getDistricts(){
		$this->_connection->Open();
		$sqlString = "select * from district";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'district');
	}
	
	public function getLocations(){
		$this->_connection->Open();
		$sqlString = "select * from location";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'location');
	}
	
	public function getDoctors(){
		$this->_connection->Open();
		$sqlString = "select * from doctors";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'doctor');
	}
	
	public function getDoctorByName($doctorName){
		$this->_connection->Open();
		$sqlString = "select * from doctors where CONCAT_WS(' ',first_name, last_name) LIKE '%{$doctorName}%'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'doctor');
	}
	
	public function GetDoctorsByDistrict($district_id){
		$this->_connection->Open();
		$sqlString = "select * from doctors d join location l on d.location_id = l.id where district_id = '{$district_id}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'doctor');
	}
	
	public function GetDoctorById($doctorId){
		$this->_connection->Open();
		$sqlString = "select * from doctors where id = '{$doctorId}";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'doctor');
	}
	
	public function getDoctorsByPostalNumberOrLocationName($searchString){
		$this->_connection->Open();
		$sqlString = "select * from doctors d join location l on d.location_id = l.id where (l.postal_number = '{$searchString}') OR l.city_name LIKE '%{$searchString}%'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'doctor');
	}
	
	public function addDoctor($doctor){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "insert into doctors(id,first_name,last_name,sex,address,house_number,location_id,telephone,email,registration,activated,specialization) values
			($doctor->id,$doctor->first_name,$doctor->last_name,$doctor->sex,$doctor->address,$doctor->house_number,$doctor->location_id,$doctor->telephone,$doctor->email,$doctor->registration,$doctor->activated,$doctor->specialization)";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully added the records to the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function addUser($user){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "insert into users(id,first_name,last_name,nickname,sex) values ($user->id,$user->first_name,$user->last_name,$user->nickname,$user->sex)";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully added the records to the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function addLocation($location){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "insert into location(id,district_id,postal_number,city_name) values ($location->id,$location->district_id,$location->postal_number,$location->city_name)";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully added the records to the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function addDistrict($district){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "insert into district(district_id,district_name) values ($location->district_id,$location->district_name)";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully added the records to the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function removeDoctor($doctorId){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "delete from doctors where id = '{$doctorId}'";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully removed the records from the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function removeUser($userId){
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "delete from users where id = '{$userId}'";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully removed the records from the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}
	
	public function updateDoctor($doctor) {
		$db_handle = mysql_connect($server,$user_name,$password);
		$db_found = mysql_select_db($database,$dbhandle);
		
		if($db_found){
			$SQL = "update ";
			$result = mysql_query(SQL);
			mysql_close($db_handle);
			//print "Successfully removed the records from the database";
		}
		else {
			//print "Database not found";
			mysql_close($db_handle);
		}
	}

	
	//MAPPERS
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	private function Map($data,$resultType){
		switch ($resultType){
			case 'user':
				$object = new User($data->id,$data->first_name,$data->last_name,$data->username,$data->password,$data->sex);
				break;
			case 'doctor':
				$object = new Doctor($data->id,$data->first_name,$data->last_name,$data->username,$data->password,$data->sex,$data->address,$data->house_number,$data->location_id,$data->telephone,$data->email,$data->registration,$data->activated,$data->specialization);
				break;
			case 'district':
				$object = new District($data->district_id,$data->district_name);
				break;
			case 'location':
				$object = new Location($data->id,$data->district_id,$data->postal_number,$data->city_name);
				break;
		}
		return $object;
	}
}